package com.sunshine.lnuplus.utils;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;

import com.sunshine.lnuplus.R;

public class DialogFragmentHelper extends DialogFragment {
    private static String title,message,positive,negative;
    private static DialogInterface.OnClickListener onPositiveClickListener,onNegativeClickListener,onSingleClickListener;
    private static DialogInterface.OnMultiChoiceClickListener onMultiClickListener;
    private static View view;
    private static boolean cancelable = false;
    private static String[] chooseString;
    private static int what = 0;
    private static boolean[] multiBoolean;
    /**
     * 这个可以在创建view前来设置信息
     * @return
     */
    public static DialogFragmentHelper newInstance(String title1, String message1, String positive1, String negative1,boolean cancelable1,
                                                   DialogInterface.OnClickListener onPositiveClickListener1, DialogInterface.OnClickListener onNegativeClickListener1){
        DialogFragmentHelper dialogFragmentHelper = new DialogFragmentHelper();
        title = title1;
        message = message1;
        positive = positive1;
        negative = negative1;
        cancelable = cancelable1;
        onPositiveClickListener = onPositiveClickListener1;
        onNegativeClickListener = onNegativeClickListener1;
        what = 1;
        return dialogFragmentHelper;
    }

    public static DialogFragmentHelper newInstance(String title1,String positive1,String negative1, View view1, boolean cancelable1,DialogInterface.OnClickListener onPositiveClickListener1,DialogInterface.OnClickListener onNegativeClickListener1){
        DialogFragmentHelper dialogFragmentHelper = new DialogFragmentHelper();
        title = title1;
        positive = positive1;
        negative = negative1;
        view = view1;
        cancelable = cancelable1;
        onPositiveClickListener = onPositiveClickListener1;
        onNegativeClickListener = onNegativeClickListener1;
        what = 2;
        return dialogFragmentHelper;
    }

    public static DialogFragmentHelper newInstance(String title1, String positive1, String negative1, boolean cancelable1, String[] chooseString1, DialogInterface.OnClickListener onSingleClickListener1,
                                                   DialogInterface.OnClickListener onPositiveClickListener1, DialogInterface.OnClickListener onNegativeClickListener1){
        DialogFragmentHelper dialogFragmentHelper = new DialogFragmentHelper();
        title = title1;
        positive = positive1;
        negative = negative1;
        cancelable = cancelable1;
        chooseString = chooseString1;
        onSingleClickListener = onSingleClickListener1;
        onPositiveClickListener = onPositiveClickListener1;
        onNegativeClickListener = onNegativeClickListener1;
        what = 3;
        return dialogFragmentHelper;
    }

    public static DialogFragmentHelper newInstance(String title1,String positive1, String negative1,  boolean cancelable1, String[] chooseString1, DialogInterface.OnMultiChoiceClickListener onMultiClickListener1,boolean[] multiBoolean1,
                                                   DialogInterface.OnClickListener onPositiveClickListener1, DialogInterface.OnClickListener onNegativeClickListener1){
        DialogFragmentHelper dialogFragmentHelper = new DialogFragmentHelper();
        title = title1;
        positive = positive1;
        negative = negative1;
        cancelable = cancelable1;
        chooseString = chooseString1;
        onMultiClickListener = onMultiClickListener1;
        multiBoolean = multiBoolean1;
        onPositiveClickListener = onPositiveClickListener1;
        onNegativeClickListener = onNegativeClickListener1;
        what = 4;
        return dialogFragmentHelper;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return super.onCreateView(inflater, container, savedInstanceState);
    }

    @NonNull
    @Override
    public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
        AlertDialog dialog = null;
        switch (what){
            case 1:
                builder.setTitle(title);
                builder.setMessage(message);
                builder.setPositiveButton(positive,onPositiveClickListener);
                builder.setNegativeButton(negative,onNegativeClickListener);
                dialog = builder.create();
                break;
            case 2:
                builder.setTitle(title);
                builder.setPositiveButton(positive,onPositiveClickListener);
                builder.setNegativeButton(negative,onNegativeClickListener);
                dialog = builder.create();
                ViewGroup viewGroup = (ViewGroup) view.getParent();
                if(viewGroup!=null) viewGroup.removeAllViews();
                dialog.setView(view);
                break;
            case 3:
                builder.setTitle(title);
                builder.setSingleChoiceItems(chooseString,0,onSingleClickListener);
                builder.setPositiveButton(positive,onPositiveClickListener);
                builder.setNegativeButton(negative,onNegativeClickListener);
                dialog = builder.create();
                break;
            case 4:
                builder.setTitle(title);
                builder.setMultiChoiceItems(chooseString,multiBoolean,onMultiClickListener);
                builder.setPositiveButton(positive,onPositiveClickListener);
                builder.setNegativeButton(negative,onNegativeClickListener);
                dialog = builder.create();
                break;
        }
        dialog.setCancelable(cancelable);
//        WindowManager.LayoutParams params = dialog.getWindow().getAttributes();
//        params.gravity = Gravity.CENTER;
//        params.windowAnimations = R.style.left_to_right_animation;
//        dialog.getWindow().setAttributes(params);
        dialog.getWindow().setBackgroundDrawableResource(R.drawable.dialog_default_bg);
        return dialog;
    }

    @Override
    public void onStart() {
        super.onStart();
        getDialog().getWindow().setLayout((int) (getActivity().getWindowManager().getDefaultDisplay().getWidth()*0.8), WindowManager.LayoutParams.WRAP_CONTENT);
    }
}
